var http = require('http'),
	url = require("url"),
	querystring = require("querystring"),
	rest = require('restler'),
	interFaceServer = 'http://115.29.138.87:7777';
http.createServer(function(req, res) {
	// 设置接收数据编码格式为 UTF-8
	req.setEncoding('utf-8');
	var arg = url.parse(req.url, true).query;
	var cback = arg.callback;

	var postData = "";
	var postDataJSON = {
		code: parseInt(arg.code),
		startdate: arg.startdate,
		enddate: arg.enddate
	}
	if (arg.type) {
		postDataJSON.type = parseInt(arg.type);
		postDataJSON.date = arg.date;
		delete postDataJSON.startdate;
		delete postDataJSON.enddate;
	}
	console.log('请求参数', postDataJSON);
	// 数据块接收中
	req.addListener("data", function(postDataChunk) {
		postData += postDataChunk;
	});
	// 数据接收完毕，执行回调函数
	req.addListener("end", function() {
		rest.postJson(interFaceServer, postDataJSON).on('complete', function(data, response) {
			console.log('服务器返回数据', data);
			// 向客户端浏览器写入HTTP的头部信息
			res.writeHead(200, {
				'Content-Type': 'text/plain;charset=utf-8'
			});
			// 返回给浏览器端数据
			res.end(cback + '(' + data + ')');
		}).on('fail', function() {
			console.log('调用失败');
		}).on('error', function() {
			console.log('调用错误');
		});
	});
}).listen(8000);